﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;
using System.ComponentModel;
using System.Threading;

namespace MyWp7Brand
{
    public partial class MainPage : PhoneApplicationPage
    {

        private int progressBarCounter = 0;
        public void ShowProgressBar(bool show)
        {
            if (show == true)
            {
                Interlocked.Increment(ref progressBarCounter);
            }
            else
            {
                Interlocked.Decrement(ref progressBarCounter);
                Interlocked.CompareExchange(ref progressBarCounter, 0, -1);
            }

            if (progressBarCounter == 0)
            {
                this.ProgressBar.IsIndeterminate = false;
                this.ProgressBar.Visibility = Visibility.Collapsed;
            }
            else
            {
                this.ProgressBar.IsIndeterminate = true;
                this.ProgressBar.Visibility = Visibility.Visible;
            }
        }

        protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e)
        {
            this.State["Panorama"] = this.panorama.SelectedIndex;
            base.OnNavigatingFrom(e);
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            if (this.State.ContainsKey("Panorama"))
                this.panorama.DefaultItem = this.panorama.Items[(int) this.State["Panorama"]];
            base.OnNavigatedTo(e);
        }

        // Constructor
        public MainPage()
        {
            InitializeComponent();

            this.DataContext = this;

            FeedItem fi = new FeedItem();
            fi.Title = "Loading";
            fi.Description = "";

            List<FeedItem> lfi = new List<FeedItem>();
            lfi.Add(fi);

            this.Twitter.ItemsSource = lfi;
            this.Blog.ItemsSource = lfi;

            BlogRefresh_Click(null, null);
            TwitterRefresh_Click(null, null);
        }

        private void BlogRefresh_Click(object sender, RoutedEventArgs e)
        {
            var rssClient = new RssClient("http://twitter.com/statuses/user_timeline/185968571.rss", this.Blog); //mettere il proprio blog
            rssClient.ItemsReceived += new RssClient.ItemsReceivedDelegate(rssClient_ItemsReceived);
            rssClient.LoadItems();
            this.ShowProgressBar(true);
        }

        private void TwitterRefresh_Click(object sender, RoutedEventArgs e)
        {
            var twiClient = new RssClient("http://twitter.com/statuses/user_timeline/185968571.rss", this.Twitter); //mettere l'RSS del proprio twitter
            twiClient.ItemsReceived += new RssClient.ItemsReceivedDelegate(rssClient_ItemsReceived);
            twiClient.LoadItems();
            this.ShowProgressBar(true);
        }

        private void Marketplace_Click(object sender, RoutedEventArgs e)
        {
            MarketplaceSearchTask mr = new MarketplaceSearchTask();
            mr.SearchTerms = "BlahBlahBlah"; // mettere il proprio nome del Marketplace
            mr.Show();
        }
 
        void rssClient_ItemsReceived(RssClient client, IList<FeedItem> items, object control)
        {
                Dispatcher.BeginInvoke(delegate()
                    {
                        if (items != null)
                        {
                            ((ItemsControl)control).ItemsSource = items;
                        }
                        else
                        {
                            if (((ItemsControl)control).Items.Count == 1)
                            {
                                FeedItem fi = new FeedItem();
                                fi.Title = "No connection";
                                fi.Description = "";

                                List<FeedItem> lfi = new List<FeedItem>();
                                lfi.Add(fi);

                                ((ItemsControl)control).ItemsSource = lfi;
                            }
                        }
                        this.ShowProgressBar(false);
                    });            
        }

        private void listbox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                FeedItem item = (FeedItem)e.AddedItems[0];
                WebBrowserTask wbt = new WebBrowserTask();
                wbt.URL = item.Link;
                wbt.Show();
            }

        }

        private void Email_Click(object sender, RoutedEventArgs e)
        {
            EmailComposeTask email = new EmailComposeTask();
            email.To = "MyWp7Brand@blah.blah"; //mettere la propria mail
            email.Subject = "Mail da My Wp7 Brand App";
            email.Show();
        }
    }
}